# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=libmbd
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.12.8
pkgrel=1
pkgdesc="Many-body dispersion library (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://github.com/libmbd/libmbd'
license=('spdx:MPL-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-*  ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/libmbd/libmbd/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-missing-deallocation.patch::https://github.com/libmbd/libmbd/commit/a4ca582e.patch"
        "002-fix-install-dll.patch")
sha256sums=('c50a61068d7aeb1ff76c32dcbf6aae848e47972bdbcfe609bd6050e853f76b1e'
            '4c155686cde3755f7f27a0ebcdf7c4aca8bcb05273f35c2a13febd9f38a51882'
            '1aa0d20789428e0c9ddff2baf4b3e001206fca0487a5334b7b3beedfce137ef7')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/001-fix-missing-deallocation.patch
  patch -p1 -i "${srcdir}"/002-fix-install-dll.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBLA_VENDOR=OpenBLAS \
      -DBUILD_TESTING=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
